<?php

/**
  *
  * Database method wrapper class for PEAR::MDB2
  * PHP versions 4 and 5
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  * (see nucleus/documentation/index.html#license for more info)
  *
  * @author    shizuki
  * @copyright 2007 shizuki
  * @license   http://www.gnu.org/licenses/gpl.txt  GNU GENERAL PUBLIC LICENSE Version 2, June 1991
  * @version   0.0.1
  * @link      http://shizuki.kinezumi.net/
  *
  * 0.0.1      Initial version.
  *
  **/

require_once('MDB2.php');

class databaseMethod
{

var $dbh;
var $plugin;

	function databaseMethod(&$plugin, $host, $database, $user, $password)
	{
		$this->plugin =& $plugin;
		$dsn = array(
						'phptype' => 'mysql',
						'hostspec' => $host,
						'database' => $database,
						'username' => $user,
						'password' => $password,
					);
		$opt = array(
						'debug'       => 2,
						'portability' => MDB2_PORTABILITY_ALL,
					);
		$this->dbh =& MDB2::connect($dsn, $opt);
		if (PEAR::isError($this->dbh)) {
			die($this->dbh->getMessage());
		}
		if (_CHARSET == 'UTF-8') {
			$this->dbh->setCharset('utf8');
		}
		$this->dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
		$this->dbh->loadModule('Manager');
		$this->dbh->loadModule('Extended', null, false);
	}

	function _getColumnsArray($table, $select, $dataType = 'integer', $where = false)
	{
	}
	function getColumnArray($table, $select, $where = '', $order = '', $fetchMode = MDB2_FETCHMODE_ASSOC)
	{
		if ($select == '*') {
			$resType = true;
		} elseif (is_array($select)) {
			$resType = array_combine($select, $dataType);
		} else {
			$resType = array($select => $dataType);
		}
		$results = $this->db->extended->autoExecute($table, null, MDB2_AUTOQUERY_SELECT, $where, null, true, $resType);
		if (PEAR::isError($results)) {
			die($results->getMessage());
		}
		if (is_array($select) || $select == '*') {
			$ret = $results->fetchAll(MDB2_FETCHMODE_ASSOC);
		} else {
			$ret = $results->fetchCol();
		}
		return $ret;
	}

}